package br.com.fogas.medicao.activity;

import br.com.fogas.medicao.R;
import br.com.fogas.medicao.activity.dto.RetornoAutorizacaoNotaDto;
import br.com.fogas.medicao.activity.helper.SimulacaoHelper;
import br.com.fogas.medicao.application.Constants;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

public class SimulacaoMedidorFragment extends Fragment {
	
	private SimulacaoHelper helper;
	
	private RetornoAutorizacaoNotaDto retornoNota;
	
	public static final SimulacaoMedidorFragment newInstance(RetornoAutorizacaoNotaDto retornoNota) {
		Bundle bundle = new Bundle();
		bundle.putSerializable(Constants.RETORNO_AUTORIZACAO_NOTA, retornoNota);
		SimulacaoMedidorFragment fragment = new SimulacaoMedidorFragment();
		fragment.setArguments(bundle);
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.retornoNota = (RetornoAutorizacaoNotaDto) getArguments().getSerializable(Constants.RETORNO_AUTORIZACAO_NOTA);
	}
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.simulacao_medidor, container, false);
        return view;
    }
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		this.helper = new SimulacaoHelper(this, retornoNota);
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == SimulacaoHelper.CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
			if (resultCode == Activity.RESULT_OK) {
				helper.carregarImagem();
			} else if (resultCode == Activity.RESULT_CANCELED) {
				Toast.makeText(getActivity(), "Captura da imagem cancelada!", Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(getActivity(), "Falha na captura da imagem!", Toast.LENGTH_SHORT).show();
			}
		}
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		this.helper.getDatabase().fecharConexao();
	}
	
	public SimulacaoHelper getHelper() {
		return helper;
	}
}